package com.machine.filling.function.main.payChoose;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.machine.filling.R;
import com.machine.filling.base.baseMvp.BaseFragment;
import com.machine.filling.db.dao.ExceptionOrderDao;
import com.machine.filling.db.dao.TempOrderDao;
import com.machine.filling.function.init.INextFragment;
import com.machine.filling.function.main.PayRes.PayResFragment;
import com.machine.filling.function.main.erweima.ErWeiMaFragment;
import com.machine.filling.function.main.topFg.TopFgFragment;
import com.machine.filling.net.serialPort.receive.Receive_EndFueling;
import com.machine.filling.util.Common;
import com.machine.filling.util.HexUtils;

/**
 * Created by Administrator on 2018/4/11 0011.
 *
 * @description
 */
public class PayChooseFragment extends BaseFragment<PayChooseContract.Presenter> implements PayChooseContract.View, View.OnClickListener {

    private INextFragment INextFragment;
    private TextView tv_money;
    private Button bt_pay_by_zhifubao;
    private Button bt_pay_by_weixin;
    private Button bt_pay_by_cash;
    private Button bt_back;

    private String portNum;
    private Receive_EndFueling cmd_end;
    private boolean isOnlyCash;

    public PayChooseFragment(String portNum, Receive_EndFueling cmd_end,boolean isOnlyCash) {
        this.portNum = portNum;
        this.cmd_end = cmd_end;
        this.isOnlyCash = isOnlyCash;
    }

    @Override
    public View initViews() {
        View view = LayoutInflater.from(mActivity).inflate(R.layout.fg_fg_pay_choose, null);
        ImageView iv_img = view.findViewById(R.id.iv_img);
        tv_money = view.findViewById(R.id.tv_money);
        bt_pay_by_zhifubao = view.findViewById(R.id.bt_pay_by_zhifubao);
        bt_pay_by_weixin = view.findViewById(R.id.bt_pay_by_weixin);
        bt_pay_by_cash = view.findViewById(R.id.bt_pay_by_cash);
        bt_back = view.findViewById(R.id.bt_back);
        bt_pay_by_zhifubao.setOnClickListener(this);
        bt_pay_by_weixin.setOnClickListener(this);
        bt_pay_by_cash.setOnClickListener(this);
        bt_back.setOnClickListener(this);
        return view;
    }

    @Override
    public void initDatas() {

        if (isOnlyCash){
            bt_pay_by_weixin.setVisibility(View.GONE);
            bt_pay_by_zhifubao.setVisibility(View.GONE);
            bt_pay_by_cash.setVisibility(View.GONE);
            bt_back.setVisibility(View.VISIBLE);
            if (Common.IS_EXCETPION_ORDER_USE) {
                tv_money.setText("油机异常,请使用现金支付: \n" + HexUtils.formatDouble(Double.parseDouble(ExceptionOrderDao.getInstance().queryByPortNum(portNum).getAmount()), 2) + "元");
            } else {
                tv_money.setText("油机异常,请使用现金支付: \n" + HexUtils.formatDouble(Double.parseDouble(TempOrderDao.getInstance().queryByPortNum(portNum).getPayTotalMoney()), 2) + "元");
            }
        }else {
            bt_pay_by_weixin.setVisibility(View.VISIBLE);
            bt_pay_by_zhifubao.setVisibility(View.VISIBLE);
            bt_pay_by_cash.setVisibility(View.VISIBLE);
            bt_back.setVisibility(View.GONE);
            if (Common.IS_EXCETPION_ORDER_USE) {
                tv_money.setText("支付金额: " + HexUtils.formatDouble(Double.parseDouble(ExceptionOrderDao.getInstance().queryByPortNum(portNum).getAmount()), 2) + "元");
            } else {
                tv_money.setText("支付金额: " + HexUtils.formatDouble(Double.parseDouble(TempOrderDao.getInstance().queryByPortNum(portNum).getPayTotalMoney()), 2) + "元");
            }
        }
    }

    public void setINextFragment(INextFragment INextFragment) {
        this.INextFragment = INextFragment;
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {// 1支付宝 2微信 3现金 4油卡
            case R.id.bt_pay_by_zhifubao:
                intent.putExtra("payType", Common.PAY_MODE_ZHIFUBAO);
                INextFragment.replaceNext(intent, ErWeiMaFragment.class.getName());
                break;
            case R.id.bt_pay_by_weixin:
                intent.putExtra("payType", Common.PAY_MODE_WEIXIN);
                INextFragment.replaceNext(intent, ErWeiMaFragment.class.getName());
                break;
            case R.id.bt_pay_by_cash:
                intent.putExtra("payType", Common.PAY_MODE_CASH);
                INextFragment.replaceNext(intent, PayResFragment.class.getName());
                break;
            case R.id.bt_back:
                intent.putExtra("payType", Common.PAY_MODE_CASH);
                INextFragment.replaceNext(intent, TopFgFragment.class.getName());
                break;
        }
    }

    @Override
    public String getPortNum() {
        return portNum;
    }

}
